package com.ie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ie.entity.MyUser;
import com.ie.mapper.UserMapper;
import com.ie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
public class MyUserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    @PostMapping("/testMapperSave")
    public MyUser testMapperSave() {
        MyUser mu = new MyUser();
        mu.setUname("3+4");
        mu.setUsex("女");
        int result = userMapper.insert(mu);
        //实体类主键属性使用@TableId注解后，主键自动回填
        return mu;
    }

    @GetMapping("/testMapperDelete")
    public int testMapperDelete() {
        List<Long> list = Arrays.asList(27L, 55L, 97L);
        int result = userMapper.deleteBatchIds(list);
        return result;
    }

    @GetMapping("/testMapperUpdate")
    public MyUser testMapperUpdate() {
        MyUser mu = new MyUser();
        mu.setUid(1);
        mu.setUname("李四3+4");
        mu.setUsex("男");
        int result = userMapper.updateById(mu);
        return mu;
    }

    @GetMapping("/testServiceSave")
    public List<MyUser> testServiceSave() {
        MyUser mu1 = new MyUser();
        mu1.setUname("testServiceSave陈恒1");
        mu1.setUsex("女");
        MyUser mu2 = new MyUser();
        mu2.setUname("testServiceSave陈恒2");
        mu2.setUsex("男");
        List<MyUser> list = Arrays.asList(mu1, mu2);
        boolean result = userService.saveBatch(list);
        return list;
    }

    @GetMapping("/testServiceUpdate")
    public List<MyUser> testServiceUpdate() {
        MyUser mu1 = new MyUser();
        mu1.setUid(98);
        mu1.setUname("3+4,110");
        mu1.setUsex("女");
        MyUser mu2 = new MyUser();
        mu2.setUid(94);
        mu2.setUname("3+4,220");
        mu2.setUsex("男");
        List<MyUser> list = Arrays.asList(mu1, mu2);
        boolean result = userService.updateBatchById(list);
        return list;
    }

    @GetMapping("/testServicePage")
    public List<MyUser> testServicePage() {
        //1为当前页，5为页面大小
        IPage<MyUser> iPage = new Page<>(1, 5);
        //条件构造器
        QueryWrapper<MyUser> wrapper = new QueryWrapper<>();
        wrapper.like("uname", "陈");
        wrapper.eq("usex", "女");
        IPage<MyUser> page = userService.page(iPage, wrapper);

//        IPage<MyUser> page = userService.page(iPage);
//        System.out.println(page.getPages());
        //返回当前页的记录
        return page.getRecords();
    }

    @GetMapping("/myFindAll")
    public List<MyUser> myFindAll() {
        return userMapper.myFindAll();
    }

    @GetMapping("/findAll")
    public List<MyUser> findAll() {

        return userMapper.selectList(null);
    }

    @GetMapping("/getOne")
    public MyUser getOne() {

        return userMapper.selectById(1L);
    }

    @GetMapping("/findOne")
    public MyUser findOne() {
//        return userService.findOne(null);
        return null;
    }

    @PostMapping("/user/register")
    public String register(MyUser user) {
        return null;
    }
}
